﻿

// 或更换迷你 Web 主机端口
using Furion;

using IotClientService;
using IotClientService.File;

using IotClientService.Models.Option;
using IotClientService.RealDbs;
using IotClientWeb;
using IotClientWeb.Interface;
using IotClientWeb.Options;
using IotClientWeb.Services;
using IotContract.Interfaces.Db;
using IotContract.Interfaces.File;
using IotContract.Interfaces.Http;
using Microsoft.Extensions.DependencyInjection;
using XT.Common.Config;
using XT.Common.Models.WinService;
using XT.Common.Services;
using XT.WinService;




string ext = string.Empty;
var location = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "-");
var locations = location.Split("-").ToList();
locations.RemoveAt(locations.Count - 1);


if (locations.Count < 4)
{
    ext = location;
}
else
{
    ext = string.Join("-", locations.TakeLast(3));
}

//var task=Task.Run(async () =>
//{
//    while (App.HttpContext==null)
//    {
//        await Task.Delay(200);
//    }
//   ShareService.RunWinService(App.RootServices, new ServiceInfo
//   {
//       Description = "IotClientWeb服务",
//       DisplayName = "IotClientWeb-" + ext,
//       Name = "IotClientWeb-" + ext
//   });

//});
// task.ContinueWith(t =>
//{
//    Console.WriteLine("IotClientWeb Started");
//});



var options = RunOptions.Default.AddWebComponent<WebComponent>();
options.Silence(true);

var host= Serve.Run(options);

ShareService.RunWinService(host.Services, new ServiceInfo
{
    Description = "IotClientWeb服务",
    DisplayName = "IotClientWeb-" + ext,
    Name = "IotClientWeb-" + ext
});

Console.ReadLine();





public class WebComponent : IWebComponent
{
    public void Load(WebApplicationBuilder builder, ComponentContext componentContext)
    {
        // 
        builder.Logging.AddFilter((provider, category, logLevel) =>
        {
            return !new[] { "Microsoft.Hosting", "Microsoft.AspNetCore" }.Any(u => category.StartsWith(u)) && logLevel >= LogLevel.Information;
        });

        // 
        builder.Configuration.Get<WebHostBuilder>().ConfigureKestrel(u =>
        {
            //u.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);
            //u.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(30);
            u.Limits.MaxRequestBodySize = null;
        });

        
    }
}






